package com.by.web;

import com.by.web.entity.TResource;
import com.by.web.service.IMenuService;
import com.by.web.vo.OnlineDTO;
import com.by.web.vo.OnlineVO;
import com.by.web.vo.SettingVO;
import org.apache.shiro.session.Session;
import org.crazycake.shiro.RedisSessionDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

@Controller
@SuppressWarnings({"all"})
@RequestMapping("/super")
public class SuperController {

	private Logger log = LoggerFactory.getLogger(SuperController.class);

	private static final String SUPER_PREFIX = "/super";

	/**
	 * 跳转在线用户页面
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("/online")
//	@RequiresRoles(value = {"ROLE_SUPER"}, logical = Logical.OR)
	public String onlinePage(OnlineVO onlineVO, HttpServletRequest request, HttpServletResponse response, Model model) {

		Collection<Session> sessions = sessionDAO.getActiveSessions();
		for (Session session : sessions){
			Collection<Object> keys = session.getAttributeKeys();
			for (Object key : keys) {
				log.debug("keys:{}", key);
				Object obj = session.getAttribute(key);
				System.out.println(obj);
			}
		}

	    ServletWebRequest webRequest = new ServletWebRequest(request, response);

		OnlineDTO onlineDTO = new OnlineDTO();
		BeanUtils.copyProperties(onlineVO, onlineDTO);
		onlineDTO.setCount(100);

		model.addAttribute("onlineDTO", onlineDTO);

		return SUPER_PREFIX + "/online";
	}

	/**
	 * 系统参数设定页面
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("/setting")
//	@RequiresRoles(value = {"ROLE_SUPER"}, logical = Logical.OR)
	public String settingPage(HttpServletRequest request, HttpServletResponse response, Model model) {
		ServletWebRequest webRequest = new ServletWebRequest(request, response);

		return SUPER_PREFIX + "/setting";
	}

	/**
	 * 系统菜单页面设定
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("/menus")
//	@RequiresRoles(value = {"ROLE_SUPER"}, logical = Logical.OR)
    public String menuPage(HttpServletRequest request, HttpServletResponse response, Model model) {
        ServletWebRequest webRequest = new ServletWebRequest(request, response);

        model.addAttribute("menus", menuService.menus_list());

        return SUPER_PREFIX + "/menus";
    }

    @GetMapping("/menus/data")
    //	@RequiresRoles(value = {"ROLE_SUPER"}, logical = Logical.OR)
    public SimpleResponse menuDataPage(HttpServletRequest request, HttpServletResponse response, Model model) {

        return new SimpleResponse(0, menuService.menus_list(), true);
    }

	/**
	 * 修改系统参数设定
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping("/setting")
	@ResponseBody
//	@RequiresRoles(value = {"ROLE_SUPER"}, logical = Logical.OR)
	public SimpleResponse settingUp(@RequestBody @Valid SettingVO settingVO, BindingResult result,HttpServletRequest request, HttpServletResponse response ) {
		//ServletWebRequest webRequest = new ServletWebRequest(request, response);

		if(result.hasErrors()) {
			return new SimpleResponse(1, "校验失败" ,false);
		}

		return new SimpleResponse(0, "" ,true);
	}

	/**
	 * 强制用户下线
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("/offline/{id}")
//	@RequiresRoles(value = {"ROLE_SUPER"}, logical = Logical.OR)
	@ResponseBody
	public SimpleResponse offlinePage(@PathVariable("id") String id,HttpServletRequest request, HttpServletResponse response) {
		String userName = ServletRequestUtils.getStringParameter(request,"name", "");

		return new SimpleResponse(HttpStatus.OK.value(), String.format("%s已经被强制下线", userName), Boolean.TRUE);
	}

	@Autowired
	private RedisSessionDAO sessionDAO;

	@Autowired
    private IMenuService menuService;

}
